package com.lepu.app.widget;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.ui.auto_viewpager.AutoScrollViewPager;
import com.core.lib.utils.DensityUtil;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.knowledge.bean.ScrollImage;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.main.activity.AppImageBroswerActivity;
import com.lepu.app.main.activity.MainTabActivity;

public class ImageHorizontalScrollView extends LinearLayout {

	private Context mContext;
	private ArrayList<ScrollImage> mScrollImageList;

	public AutoScrollViewPager mAutoScrollViewPager;
	public LinearLayout mImageScrollLayout;

	public ImageHorizontalScrollView(Context context,
			ArrayList<ScrollImage> dataList) {
		super(context);

		mContext = context;
		mScrollImageList = dataList;

		LayoutInflater.from(mContext).inflate(R.layout.main_image_scroll_view,
				this, true);
		init();
	}

	public ImageHorizontalScrollView(Context context, AttributeSet attrs,
			ArrayList<ScrollImage> dataList) {
		super(context, attrs);

		mContext = context;
		mScrollImageList = dataList;

		LayoutInflater.from(mContext).inflate(R.layout.main_image_scroll_view,
				this, true);
		init();
	}

	private void init() {
		mImageScrollLayout = (LinearLayout) findViewById(R.id.scroll_image_layout);

		int wh = DensityUtil.dip2px(mContext, 150);
		for (int i = 0; i < mScrollImageList.size(); i++) {
			ImageView image = new ImageView(mContext);
			if (mScrollImageList.get(i).imageUrl != null) {

				image.setLayoutParams(new ViewGroup.LayoutParams(wh, wh));
				// 加载图片
				String imageUrl = mScrollImageList.get(i).imageUrl;
				UniversalImageLoadTool.disPlay(imageUrl, image,
						R.drawable.default_icon_small);
			} else if (mScrollImageList.get(i).imageId != 0) {

				image.setLayoutParams(new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.WRAP_CONTENT,
						ViewGroup.LayoutParams.WRAP_CONTENT));

				image.setBackgroundResource(mScrollImageList.get(i).imageId);
			}
			image.setOnClickListener(new MyViewOnclicklistener(mScrollImageList
					.get(i)));
			mImageScrollLayout.addView(image);
		}

		// test
//		final HorizontalScrollView hScrollView = (HorizontalScrollView) findViewById(R.id.hScrollView);
//
//		new Handler().postDelayed((new Runnable() {
//			@Override
//			public void run() {
//				hScrollView.scrollTo(mImageScrollLayout
//						.getChildAt(mScrollImageList.size()-1).getLeft() - 100, 0);
//			}
//		}), 5);
	}

	class MyViewOnclicklistener implements View.OnClickListener {
		private ScrollImage scrollImage;

		public MyViewOnclicklistener(ScrollImage scrollImage) {
			this.scrollImage = scrollImage;
		}

		@Override
		public void onClick(View v) {
			if (!TextUtils.isEmpty(scrollImage.contentUrl)) {
				Intent intentRead1 = new Intent(mContext,
						MyArticleInfoActivity.class);
				intentRead1.putExtra("url", scrollImage.contentUrl);
				intentRead1.putExtra("title", scrollImage.title);
				MainTabActivity.getInstance().startActivity(intentRead1, true);
			} else if (!TextUtils.isEmpty(scrollImage.content)) {

			} else if (!TextUtils.isEmpty(scrollImage.imageUrl)) {
				int index = 0;
				String[] urlArray = new String[mScrollImageList.size()];

				for (int i = 0; i < mScrollImageList.size(); i++) {
					String imageUrl = mScrollImageList.get(i).imageUrl;
					urlArray[i] = imageUrl;

					if (scrollImage.imageUrl.equals(imageUrl)) {
						index = i;
					}
				}

				Intent intent = new Intent(mContext,
						AppImageBroswerActivity.class);
				intent.putExtra("isShowTopbar", false);
				intent.putExtra("index", index);
				intent.putExtra("urlArray", urlArray);
				((BaseFragmentActivity) mContext).startActivity(intent);
			}
		}
	}

}
